package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.MyOrderInfoBean;
import com.mz.ozy.bean.QiNiuBean;
import com.mz.ozy.bean.UploadBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.ReturnModel;
import com.mz.ozy.mvp.view.EvaluateContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.RxUtil;
import com.mz.ozy.utils.ToastUtils;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;


public class EvaluatePresenter extends BasePresenterImpl<EvaluateContract.View> implements EvaluateContract.Presenter {

    private ReturnModel mModel;
    private int index;
    private List<QiNiuBean> mPicList;
    private String comcommentPics;

    public EvaluatePresenter() {
        mModel = new ReturnModel();
    }

    @Override
    public void initData(Activity activity, String tesq) {

        Map<String, String> maps = new HashMap<>();
        maps.put("tesq", tesq);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10021, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<MyOrderInfoBean>(mView) {
                    @Override
                    public void onSuccesss(MyOrderInfoBean myOrderInfoBean) {

                        mView.onInitSuccess(myOrderInfoBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                        mView.onInitError("");
                    }
                });
    }

    @Override
    public void onEvaluate(Context context, String scid, List<QiNiuBean> list, String content, String currScore, String coachScore) {
        mPicList = list;
        comcommentPics = "";
        index = 0;

        if (mPicList.size() == 0) {
            //发起评价
            onEvaluateContent(context, scid, content, currScore, coachScore);
        } else {
            uploadImg(context, scid, content, currScore, coachScore);
        }
    }

    @Override
    public void onEvaluate1(Context context, String tesq, String consistent, String logistics, String attitude, String plist, int beanType) {
        mModel.getListDetail(context, tesq, consistent, logistics, attitude, plist, beanType)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.up(jsonObject);
                    }
                });
    }


    private void uploadImg(Context context, String scid, String content, String currScore, String coachScore) {
        File file = new File(mPicList.get(index).getUrl());
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        RetrofitUtils.getApiService(context).upload(body)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            UploadBean bean = new Gson().fromJson(responseBody.string(), UploadBean.class);
                            if (!TextUtils.isEmpty(bean.getData().getSrc())) {
                                index++;
                                if (!TextUtils.isEmpty(comcommentPics)) {
                                    comcommentPics = comcommentPics + "," + bean.getData().getSrc();
                                } else {
                                    comcommentPics = bean.getData().getSrc();
                                }
                                if (index != mPicList.size()) {
                                    //继续上传下一张图片
                                    uploadImg(context, scid, content, currScore, coachScore);
                                } else {
                                    //上传成功,发表评价
                                    onEvaluateContent(context, scid, content, currScore, coachScore);
                                }
                            } else {
                                //失败
                                ToastUtils.showToast("上传失败");
                                mView.dismissLoading();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        // mView.dismissLoading();
                    }
                });
    }


    private void onEvaluateContent(Context context, String scid, String content, String currScore, String coachScore) {
      /*  mModel.onEvaluate(context, scid,comcommentPics,content,currScore,coachScore)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView){

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onEvaluateSuccess();
                    }
                });*/
    }

}
